﻿using Microsoft.Extensions.DependencyModel;
using NPOI.SS.Formula.Functions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace LanDBom.Models.Procure
{
    public class Find_APP_information
    {
        LDbaseEntities LD = new LDbaseEntities();


        public List<Applications> find_app_information(string APP_bian, string App_KHname, string App_xiang)
        {
            List<Applications> appList = LD.Applications.Where(x=>(x.App_bu=="5"|| x.App_bu == "3"|| x.App_bu == "4")&&x.AP_bian.Contains("SQ")&&x.AP_QTJY==null).OrderByDescending(s=>s.App_qishi).ToList();
            try
            {
                foreach(var app in appList)
                {
                    if (app.AP_bian == null)
                    {
                        app.AP_bian = "";
                    }
                    if (app.App_Kehu == null)
                    {
                        app.App_Kehu = "";
                    }
                    if (app.App_qishi == null)
                    {
                        app.App_qishi = "";
                    }
                    if (app.App_BanB == null)
                    {
                        app.App_BanB = "1";
                    }
                }
                if (APP_bian != "")
                {
                    appList = appList.Where(x => x.AP_bian.Contains(APP_bian)).ToList();
                }
                if (App_KHname != "" && App_xiang != null)
                {
                    appList = appList.Where(a => a.App_Kehu.Contains(App_KHname)).ToList();
                }
                if (App_xiang != "" && App_xiang != null)
                {
                    appList = appList.Where(x => x.APP_name.Contains(App_xiang)).ToList();
                }
                appList = appList.GroupBy(d => new { d.AP_bian }).Select(a => a.OrderByDescending(b => b.App_BanB).ElementAt(0)).ToList();
                return appList;
            }
            catch (Exception)
            {
                throw;
            }
        }
        public List<Applications> find_app_information2(string APP_bian, string App_KHname, string App_xiang)
        {
            List<Applications> appList = LD.Applications.Where(x => (x.App_bu == "3"|| x.App_bu == "4")&&x.AP_bian.Contains("SQ")&&x.AP_QTJY==null).OrderByDescending(s=>s.App_qishi).ToList();
            try
            {
                foreach (var app in appList)
                {
                    if (app.AP_bian == null)
                    {
                        app.AP_bian = "";
                    }
                    if (app.App_Kehu == null)
                    {
                        app.App_Kehu = "";
                    }
                    if (app.App_qishi == null)
                    {
                        app.App_qishi = "";
                    }
                    if (app.App_BanB == null)
                    {
                        app.App_BanB = "1";
                    }
                }
                if (APP_bian != "")
                {
                    appList = appList.Where(x => x.AP_bian.Contains(APP_bian)).ToList();
                }
                if (App_KHname != "" && App_xiang != null)
                {
                    appList = appList.Where(a => a.App_Kehu.Contains(App_KHname)).ToList();
                }
                if (App_xiang != "" && App_xiang != null)
                {
                    appList = appList.Where(x => x.APP_name.Contains(App_xiang)).ToList();
                }
                appList = appList.GroupBy(d => new { d.AP_bian }).Select(a => a.OrderByDescending(b => b.App_BanB).ElementAt(0)).ToList();
                return appList;
            }
            catch (Exception)
            {
                throw;
            }
        }
        public List<Applications> find_app_infor111(string APP_bian, string App_KHname, string App_xiang)
        {
            List<Applications> appList = LD.Applications.Where(x => x.AP_bian.Contains("ERP")).ToList();
            try
            {
                if (APP_bian != "")
                {
                    appList = appList.Where(x => x.AP_bian.Contains(APP_bian)).ToList();
                }
                appList = appList.GroupBy(d => new { d.AP_bian }).Select(a => a.OrderByDescending(b => b.App_BanB).ElementAt(0)).ToList();
                
                if (App_KHname!=""&&App_KHname!=null)
                {
                    List<Applications> aapp = new List<Applications>();
                    foreach (Applications applications in appList)
                    {
                        var bom = LD.BOMini.Where(x => x.BOM2_bian == applications.AP_bian&&x.BOM2_liao==App_KHname).ToList();
                        int dd = 0;
                        if (bom.Count() > 0)
                        {
                            aapp.Add(applications);
                        }
                    }
                    appList=aapp.GroupBy(d => new { d.AP_bian }).Select(a => a.OrderByDescending(b => b.App_BanB).ElementAt(0)).ToList();
                }
                return appList;
            }
            catch (Exception)
            {
                throw;
            }
        }
        public List<Applications> find_app_infor(string APP_bian, string App_KHname, string App_xiang)
        {
            List<Applications> appList = LD.Applications.Where(x => x.App_bu == "2"&&x.App_zhand!="3"&&x.AP_bian.Contains("SQ") ).OrderByDescending(s=>s.App_qishi).ToList();
            appList = appList.Where(s => s.AP_QTJY == null).ToList();
            try
            {
                foreach (var app in appList)
                {
                    if (app.AP_bian == null)
                    {
                        app.AP_bian = "";
                    }
                    if (app.App_Kehu == null)
                    {
                        app.App_Kehu = "";
                    }
                    if (app.App_qishi == null)
                    {
                        app.App_qishi = "";
                    }
                    if (app.App_BanB == null)
                    {
                        app.App_BanB = "1";
                    }
                }
                if (APP_bian != "")
                {
                    appList = appList.Where(x => x.AP_bian.Contains(APP_bian)).ToList();
                }
                if (App_KHname != "" && App_xiang != null)
                {
                    appList = appList.Where(a => a.App_Kehu.Contains(App_KHname)).ToList();
                }
                if (App_xiang != "" && App_xiang != null)
                {
                    appList = appList.Where(x => x.APP_name.Contains(App_xiang)).ToList();
                }
                appList = appList.GroupBy(d => new { d.AP_bian }).Select(a => a.OrderByDescending(b => b.App_BanB).ElementAt(0)).ToList();
                return appList;
            }
            catch (Exception)
            {
                throw;
            }
        }

        public List<Applications> find_app_information(string APP_bian)
        {
            List<Applications> appList_2 = LD.Applications.Where(x => x.AP_bian == APP_bian).ToList();

            return appList_2;
        }

        public List<Applications> Appindex(string aP_bian, string app_Kehu, string app_JTime)
        {
            List<Applications> app = LD.Applications.ToList();
            if (aP_bian != "" && aP_bian != null)
            {
                app = app.Where(a => a.AP_bian.Contains(aP_bian)).ToList();
            }
            if (app_Kehu != "")
            {
                app = app.Where(a => a.App_Kehu.Contains(app_Kehu)).ToList();
            }
            if (app_JTime != "")
            {
                app = app.Where(a => a.App_JTime.Contains(app_JTime)).ToList();
            }
            return app;
        }
        public List<Applications> find_app_int(string APP_bian, string App_KHname, string App_xiang)
        {
            List<Applications> appList = LD.Applications.Where(x => x.App_bu == "3").ToList();
            try
            {
                foreach (var app in appList)
                {
                    if (app.AP_bian == null)
                    {
                        app.AP_bian = "";
                    }
                    if (app.App_Kehu == null)
                    {
                        app.App_Kehu = "";
                    }
                    if (app.App_qishi == null)
                    {
                        app.App_qishi = "";
                    }
                    if (app.App_BanB == null)
                    {
                        app.App_BanB = "1";
                    }
                }
                if (APP_bian != "")
                {
                    appList = appList.Where(x => x.AP_bian.Contains(APP_bian)).ToList();
                }
                if (App_KHname != "" && App_xiang != null)
                {
                    appList = appList.Where(a => a.App_Kehu.Contains(App_KHname)).ToList();
                }
                if (App_xiang != "" && App_xiang != null)
                {
                    appList = appList.Where(x => x.APP_name.Contains(App_xiang)).ToList();
                }
                appList = appList.GroupBy(d => new { d.AP_bian }).Select(a => a.OrderByDescending(b => b.App_BanB).ElementAt(0)).ToList();
                return appList;
            }
            catch (Exception)
            {
                throw;
            }
        }
        public List<Applications> find_intsr(string BB_SQ_xmmc, string BB_SQ_khmc, string BB_SQ_gjz, string jssj, string qssj, string danhao, string xmlx,string xmjd,string jzrq,string xmjs,string MRRY,string Sheji)
        {
            List<Applications> index = LD.Applications.Where(s => s.AP_bian.Contains("SQ")&&s.AP_QTJY==null).ToList();
            if (MRRY!=null&& MRRY != "")
            {
                index = index.Where(s => s.App_fzr == MRRY).ToList();
            }
            if (Sheji != null&& Sheji != "")
            {
                index = index.Where(s => s.APP_she == Sheji).ToList();
            }
            foreach (var i in index)
            {
                if (i.App_JTime == null)
                {
                    i.App_JTime = "1/11/2100 12:00:00 AM";
                }
                if (i.App_qishi == null)
                {
                    i.App_qishi = "1/11/1111 12:00:00 AM";
                }
                if (i.APP_name == null)
                {
                    i.APP_name = "";
                }
                if (i.App_Kehu == null)
                {
                    i.App_Kehu = "";
                }
                if (i.APP_Zhong == null)
                {
                    i.APP_Zhong = "";
                }
                if (i.AP_bian == null)
                {
                    i.AP_bian = "";
                }
            }
            if (danhao != null&& danhao!="")
            {
                index = index.Where(x => x.AP_bian.Contains(danhao)).ToList();
            }
            if (BB_SQ_xmmc != ""&& BB_SQ_xmmc!=null)
            {
                index = index.Where(x => x.APP_name.Contains(BB_SQ_xmmc)).ToList();
            }
            if (BB_SQ_khmc != "" && BB_SQ_khmc != null)
            {
                index = index.Where(x => x.App_Kehu.Contains(BB_SQ_khmc)).ToList();
            }
            if (BB_SQ_gjz != null && BB_SQ_gjz != "")
            {
                index = index.Where(x => x.AP_bian.Contains(BB_SQ_gjz) || x.APP_name.Contains(BB_SQ_gjz) || x.App_Kehu.Contains(BB_SQ_gjz) || x.APP_Zhong.Contains(BB_SQ_gjz) || x.App_qishi.Contains(BB_SQ_gjz) || x.App_JTime.Contains(BB_SQ_gjz)).ToList();
            }
            if (jssj != "" && jssj != null)
            {
                DateTime result = DateTime.Parse(jssj);
                index = index.Where(x => DateTime.Parse(x.App_qishi) <= result).ToList();
            }
            if (qssj != "" && qssj != null)
            {
                DateTime result = DateTime.Parse(qssj);
                index = index.Where(x => DateTime.Parse(x.App_qishi) >= result).ToList();
            }
            if (jzrq != "" && jzrq != null)
            {
                DateTime result = DateTime.Parse(jzrq);
                index = index.Where(x => DateTime.Parse(x.App_qishi) <= result).ToList();
            }
            if (xmlx != "0" && xmlx != null)
            {
                if (xmlx == "WON Opportunities")
                {
                    index = index.Where(x => x.App_xmzt == "WON Opportunities").ToList();
                }
                else if (xmlx == "OPEN Opportunities")
                {
                    index = index.Where(x => x.App_xmzt == "OPEN Opportunities").ToList();
                }
                else if (xmlx == "LOST Opportunities")
                {
                    index = index.Where(x => x.App_xmzt == "LOST Opportunities").ToList();
                }
            }
            if (xmjs != "" && xmjs != null)
            {
                if (xmjs == "0")
                {
                    index = index.ToList();
                }
                else if (xmjs == "1")
                {
                    index = index.Where(x => x.App_bu == "-99").ToList();
                }
                else if (xmjs == "2")
                {
                    index = index.Where(x => x.App_bu != "-99").ToList();
                }
            }
            if (xmjd != null && xmjd != "")
            {
                if (xmjd == "0")
                {
                    index = index.ToList();
                }
                else  if (xmjd == "2")
                {
                    index = index.Where(x => x.App_bu == xmjd).ToList();
                }
                else if (xmjd == "3")
                {
                    index = index.Where(x => x.App_bu == xmjd).ToList();
                }
                else if (xmjd == "4")
                {
                    index = index.Where(x => x.App_bu == xmjd).ToList();
                }
                else if (xmjd == "5")
                {
                    index = index.Where(x => x.App_bu == xmjd).ToList();
                    var list2 = new List<Applications>();
                    foreach (Applications applications in index)
                    {
                        shenpingliu shenpingliu = LD.shenpingliu.Where(x => x.FJINF == applications.AP_bian && x.FMIao == "核价审批" && x.Fzhuang != "结束").FirstOrDefault();
                        if (shenpingliu == null)
                        {
                            list2.Add(applications);
                        }
                    }
                    index = list2;
                }
                else if (xmjd == "6")
                {
                    index = index.Where(x => x.App_bu == xmjd).ToList();
                    var list2 = new List<Applications>();
                    foreach (Applications applications in index)
                    {
                        shenpingliu shenpingliu = LD.shenpingliu.Where(x => x.FJINF == applications.AP_bian && x.FMIao == "对外报价" && x.Fzhuang != "结束").FirstOrDefault();
                        if (shenpingliu == null)
                        {
                            list2.Add(applications);
                        }
                    }
                    index = list2;
                }
                else if (xmjd == "7")
                {
                    index = index.Where(x => x.App_bu == xmjd).ToList();
                }
                else if (xmjd == "8")
                {
                    index = index.Where(x => x.App_bu == "5").ToList();
                    var list2 = new List<Applications>();
                    foreach(Applications applications in index)
                    {
                        shenpingliu shenpingliu = LD.shenpingliu.Where(x => x.FJINF == applications.AP_bian&&x.FMIao=="核价审批"&&x.Fzhuang!= "结束").FirstOrDefault();
                        if (shenpingliu != null)
                        {
                            list2.Add(applications);
                        }
                    }
                    index = list2;
                }
                else if (xmjd == "9")
                {
                    index = index.Where(x => x.App_bu == "6").ToList();
                    var list2 = new List<Applications>();
                    foreach (Applications applications in index)
                    {
                        shenpingliu shenpingliu = LD.shenpingliu.Where(x => x.FJINF == applications.AP_bian && x.FMIao == "对外报价" && x.Fzhuang != "结束").FirstOrDefault();
                        if (shenpingliu != null)
                        {
                            list2.Add(applications);
                        }
                    }
                    index = list2;
                }
                else 
                {
                    var shunf = xmjd.Split('_')[1];
                    var list2  = new List<Applications>();
                    foreach (Applications s in index)
                    {
                        Designdraw designdraw = LD.Designdraw.Where(x => x.D_Ton == s.AP_bian).FirstOrDefault();
                        if (designdraw != null)
                        {
                            if (designdraw.D_zhuang == shunf)
                            {
                                var SDE = LD.Applications.Where(G => G.AP_bian == s.AP_bian).FirstOrDefault();
                                list2.Add(SDE);
                            }
                        }
                    }
                    index = list2;
                }
            }
            return index;
        }
        public List<Applications> find_app_infor11(string APP_bian, string App_KHname, string App_xiang,string XMfzr)
        {
            List<Applications> appList = LD.Applications.Where(x => x.App_bu == "6" && x.App_zhand != "3"&&x.AP_bian.Contains("SQ")).OrderByDescending(s=>s.App_qishi).ToList();
            try
            {
                if (APP_bian != "")
                {
                    appList = appList.Where(x => x.AP_bian.Contains(APP_bian)).ToList();
                }
                if (App_KHname != "" && App_xiang != null)
                {
                    appList = appList.Where(a => a.App_Kehu.Contains(App_KHname)).ToList();
                }
                if (App_xiang != "" && App_xiang != null)
                {
                    appList = appList.Where(x => x.APP_name.Contains(App_xiang)).ToList();
                }
                if (XMfzr != "" && XMfzr != null)
                {
                    appList = appList.Where(x => x.App_fzr==XMfzr).ToList();
                }
                var APPL = new List<Applications>();
                foreach (var item in appList)
                {
                    var Bina = item.AP_bian;
                    var HJ = LD.shenpingliu.Where(s => s.FJINF == Bina && s.FMIao == "对外报价").FirstOrDefault();
                    if (HJ == null || HJ.FFZ == "驳回")
                    {
                        APPL.Add(item);
                    }
                }
                APPL = APPL.GroupBy(d => new { d.AP_bian }).Select(a => a.OrderByDescending(b => b.App_BanB).ElementAt(0)).ToList();
                return APPL;
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}